package cn.mdx.service;

import cn.mdx.common.utils.CheckUtil;
import cn.mdx.mapper.DemandMapper;
import cn.mdx.mapper.DemandlistMapper;
import cn.mdx.mapper.ResultMapper;
import cn.mdx.mapper.ServiceMapper;
import cn.mdx.pojo.Demand;
import cn.mdx.pojo.Demandlist;
import cn.mdx.pojo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class ResultService {
    private static final Logger logger = LoggerFactory.getLogger(ResultService.class);

    @Autowired
    private ResultMapper resultMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private DemandlistMapper demandlistMapper;

    @Autowired
    private DemandMapper demandMapper;

    public void addResult(Result result,Integer id) {
        CheckUtil.mustNullOtherOpposite(result,"参数异常","audit&uid&rid");
        CheckUtil.notNull(id,"参数异常");
        result.setUid(id);
        cn.mdx.pojo.Service service = serviceMapper.findAuditService(result.getSid());
        if(service==null) {
            logger.info("[添加失败]");
            CheckUtil.fail("该资源编号不存在");
        } else if(!service.getUid().equals(id)) {
            logger.info("[添加失败]");
            CheckUtil.fail("该资源编号不属于自己");
        }
        Demand demand = getDemand(result.getDid());
        if(demand==null) {
            logger.info("[添加失败]");
            CheckUtil.fail("该需求编号不存在");
        }
        logger.info("[添加对接成果中，参数[{},id:{}]]",result,id);
        resultMapper.insertSelective(result);
        logger.info("[添加成功]");
    }

    public void setResult(Result result,Integer id) {
        CheckUtil.mustNull(result,"参数异常","audit");
        CheckUtil.notNull(result,"参数异常","rid");
        CheckUtil.notNull(id,"参数异常");
        if(result.getSid()!=null) {
            cn.mdx.pojo.Service service = serviceMapper.findAuditService(result.getSid());
            if(service==null) {
                logger.info("[修改失败]");
                CheckUtil.fail("该资源编号不存在");
            } else if(!service.getUid().equals(id)) {
                logger.info("[修改失败]");
                CheckUtil.fail("该资源编号不属于自己");
            }
        }
        if(result.getDid()!=null){
            Demand demand = getDemand(result.getDid());
            if(demand==null) {
                logger.info("[修改失败]");
                CheckUtil.fail("该需求编号不存在");
            }
        }
        logger.info("[修改对接成果中，参数[{},id:{}]]",result,id);
        Result result1 = resultMapper.selectByPrimaryKey(result.getRid());

        resultMapper.updateByPrimaryKeySelective(result);
        logger.info("[修改成功]");
    }

    public Result getResult(Integer rid,Integer id) {
        CheckUtil.notNull(rid,"参数异常");
        CheckUtil.notNull(id,"参数异常");
        logger.info("[查询对接成果中，参数[{}]]");
        Result result = resultMapper.selectByPrimaryKey(rid);
        if(result==null || !result.getUid().equals(id)){
            CheckUtil.fail("非法访问");
        }
        logger.info("[查找成功]");
        return result;
    }

    public void delResult(Integer rid,Integer id) {
        CheckUtil.notNull(rid,"参数异常");
        CheckUtil.notNull(id,"参数异常");
        logger.info("[删除对接成果中，参数[{}]]");
        Result result = resultMapper.selectByPrimaryKey(rid);
        if(result==null || !result.getUid().equals(id)){
            CheckUtil.fail("非法访问");
        }
        resultMapper.deleteByPrimaryKey(rid);
        logger.info("[删除成功]");
    }


    public ArrayList<Result> getResultList(Integer uid){
        CheckUtil.notNull(uid,"参数异常");
        return resultMapper.getResultList(uid);
    }


    public PageInfo<Result> getResultList(Integer page,Integer aid){
        CheckUtil.notNull(page,"参数异常");
        CheckUtil.notNull(aid,"参数异常");
        PageHelper.startPage(page,10,"rid desc");
        PageInfo<Result> pageInfo = new PageInfo<Result>(resultMapper.getResultListGroupByAchievement(aid));
        return pageInfo;
    }

    public Demand getDemand(Integer id) {
        CheckUtil.notNull(id,"参数为空");
        logger.info("[查找中,参数[id:{}]]",id);
        Demandlist demandlist = demandlistMapper.selectByPrimaryKey(id);
        if(demandlist==null) {
            logger.info("[查找失败]");
            return null;
        }
        Demand demand = demandMapper.selectByPrimaryKey(demandlist.getDid());

        if(demand.getAudit()==0){
            logger.info("[查找失败]");
            return null;
        }
        logger.info("[查找成功]");
        return demand;
    }

}
